// 1. 加载React模块
import React, {Component} from 'react';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
//import Ionicons from 'react-native-vector-icons/Ionicons';
import SettingsInfoScreen from '../pages/Home/SettingInfos';
import StatisticsInfos from '../pages/Home/StatisticsInfos';
import signInfoScreen from '../pages/Home/signHome';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {getMenuAndPermissions} from '../api/user';
import {Loading} from '../utils/Loading';
import {Toast} from '../utils/Toast';
import {setPermissionsStorage} from '../storage/permissions';


const Tab = createBottomTabNavigator();

class MainTabNavigator extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        };

    }

    componentDidMount() {
        this.init();
    }

    init = () => {
        Loading.show();
        getMenuAndPermissions().then(async res=>{
            let {permissions}=res
            let data={
                expire:3600 * 12 *1000*1000 //12小时有效期
            }
            if (permissions) {
                data.permissionsStorage=permissions;
                await setPermissionsStorage(data)
            }else {
                data.permissionsStorage=[]
                await setPermissionsStorage(data)
            }
            console.log(permissions,"权限")
        }).catch(err=>{
            Loading.hidden()
            Toast.showWarning(err.toString())
        }).finally(()=>{
            Loading.hidden()
        })
    };

    render() {
        return (
            <Tab.Navigator
                screenOptions={({route, navigation}) => ({
                    headerShown: false,
                    tabBarIcon: ({focused, color, size}) => {
                        let iconName;
                        //console.log(route)
                        if (route.name === 'signHome') {
                            iconName = 'ios-locate';
                            //console.log(focused)
                        } else if (route.name === 'settingInfo') {
                            iconName = 'ios-settings';
                        } else if (route.name === 'statisticsInfos') {
                            iconName = 'md-stats-chart';
                        }
                        // You can return any component that you like here!
                        return <Ionicons name={iconName} size={size} color={color}/>;
                    },

                })}
            >
                <Tab.Screen name="signHome" options={{
                    title: '打卡',
                }} component={signInfoScreen}/>
                <Tab.Screen name="statisticsInfos" options={{
                    title: '统计',
                }} component={StatisticsInfos}/>
                <Tab.Screen name="settingInfo" options={{
                    title: '设置',
                }} component={SettingsInfoScreen}/>
            </Tab.Navigator>
        );
    }
}

export default MainTabNavigator;

